<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="item3Id">
        <el-input
          v-model="queryParams.item3Id"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物名称" prop="item2Name">
        <el-input
          v-model="queryParams.item2Name"
          placeholder="请输入货物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="itemState">
        <el-input
          v-model="queryParams.itemState"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="itemType">
        <el-input
          v-model="queryParams.itemType"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目前地点" prop="itemPlace">
        <el-input
          v-model="queryParams.itemPlace"
          placeholder="请输入目前地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预计时间" prop="itemTime">
        <el-input
          v-model="queryParams.itemTime"
          placeholder="请输入预计时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输方式" prop="itemMethod">
        <el-input
          v-model="queryParams.itemMethod"
          placeholder="请输入运输方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:flow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:flow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:flow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:flow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <br>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">动态管理</a></el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-descriptions title="货物动态详情" direction="vertical" :column="4" border/>
    <el-table v-loading="loading" :data="flowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="item3Id" />
      <el-table-column label="货物名称" align="center" prop="item2Name" />
      <el-table-column label="货物数目" align="center" prop="itemNum" />
      <el-table-column label="状态" align="center" prop="itemState" />
      <el-table-column label="类型" align="center" prop="itemType" />
      <el-table-column label="目前地点" align="center" prop="itemPlace" />
      <el-table-column label="预计时间" align="center" prop="itemTime" />
      <el-table-column label="运输方式" align="center" prop="itemMethod" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:flow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:flow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改货物动态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="货物名称" prop="item2Name">
          <el-input v-model="form.item2Name" placeholder="请输入货物名称" />
        </el-form-item>
        <el-form-item label="货物数目" prop="itemNum">
          <el-input v-model="form.itemNum" placeholder="请输入货物数目" />
        </el-form-item>
        <el-form-item label="状态" prop="itemState">
          <el-input v-model="form.itemState" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="类型" prop="itemType">
          <el-input v-model="form.itemType" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="目前地点" prop="itemPlace">
          <el-input v-model="form.itemPlace" placeholder="请输入目前地点" />
        </el-form-item>
        <el-form-item label="预计时间" prop="itemTime">
          <el-input v-model="form.itemTime" placeholder="请输入预计时间" />
        </el-form-item>
        <el-form-item label="运输方式" prop="itemMethod">
          <el-input v-model="form.itemMethod" placeholder="请输入运输方式" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlow, getFlow, delFlow, addFlow, updateFlow } from "@/api/system/flow";

export default {
  name: "Flow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货物动态表格数据
      flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        item3Id: null,
        item2Name: null,
        itemState: null,
        itemType: null,
        itemPlace: null,
        itemTime: null,
        itemMethod: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        item3Id: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        item2Name: [
          { required: true, message: "货物名称不能为空", trigger: "blur" }
        ],
        itemNum: [
          { required: true, message: "货物数目不能为空", trigger: "blur" }
        ],
        itemState: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        itemType: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        itemPlace: [
          { required: true, message: "目前地点不能为空", trigger: "blur" }
        ],
        itemTime: [
          { required: true, message: "预计时间不能为空", trigger: "blur" }
        ],
        itemMethod: [
          { required: true, message: "运输方式不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询货物动态列表 */
    getList() {
      this.loading = true;
      listFlow(this.queryParams).then(response => {
        this.flowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        item3Id: null,
        item2Name: null,
        itemNum: null,
        itemState: null,
        itemType: null,
        itemPlace: null,
        itemTime: null,
        itemMethod: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.item3Id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货物动态";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const item3Id = row.item3Id || this.ids
      getFlow(item3Id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货物动态";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.item3Id != null) {
            updateFlow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const item3Ids = row.item3Id || this.ids;
      this.$modal.confirm('是否确认删除货物动态编号为"' + item3Ids + '"的数据项？').then(function() {
        return delFlow(item3Ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/flow/export', {
        ...this.queryParams
      }, `flow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
